package com.txsj.scan.presenter;

import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.BaseListResult;
import com.txsj.scan.bean.WarehouseBean;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.view.IWarehouseView;

import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SelectWarehousePresenter extends BasePresenter<IWarehouseView> {

    public SelectWarehousePresenter(IWarehouseView view) {
        super(view);
    }

    @Override
    protected void initModel() {

    }

    /**
     * 获取仓库列表
     */
    public void getWarehouseHttp() {
        Map<String, String> params = RetrofitManager.getParams();
        params.put("is_show", "1");
        view.onLoading();
        RetrofitManager.getDefault().create(ApiServer.class).getWareHouse(RetrofitManager.getHeader(),params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseListResult<WarehouseBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseListResult<WarehouseBean> result) {
                        if (result.isSuccess()){
                            List<WarehouseBean> list = result.getData();
                            if (null == list || list.size() == 0){
                                view.onError("仓库列表不存在");
                            }else {
                                //设置数据
                                view.onSuccess(list);
                            }
                        }else {
                            view.onError("网络异常");
                            Trace.e("SelectWarehouseActivity+++++++++ " + result.getMsg());
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError("网络异常");
                        Trace.e("SelectWarehouseActivity+++++++++ " + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
